package com.wangkang.eduservice.controller;
import com.wangkang.commonutils.R;
import com.wangkang.eduservice.entity.EduChapter;
import com.wangkang.eduservice.entity.form.EduChapterForm;
import com.wangkang.eduservice.entity.vo.EduChapterVo;
import com.wangkang.eduservice.enums.EduResultCode;
import com.wangkang.eduservice.service.EduChapterService;
import com.wangkang.servicebase.exceptionhandler.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wangkang
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/eduservice/chapter")
@Api(tags = "课程章节管理")
//@CrossOrigin
public class EduChapterController {

    @Autowired
    private  EduChapterService chapterService;

    public EduChapterController(EduChapterService chapterService) {
        this.chapterService = chapterService;
    }


    @ApiOperation("根据id查询课程大纲列表")
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){

        List<EduChapterVo> chapterVoList =  chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",chapterVoList);
    }

    @ApiOperation("添加章节")
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapterForm chapterForm){
        EduChapter chapter = new EduChapter();
        BeanUtils.copyProperties(chapterForm,chapter);
        boolean saveResult = chapterService.save(chapter);
        if(!saveResult){
            throw GuliException.from(EduResultCode.SAVE_ERROR);
        }
        return R.ok();
    }

    @ApiOperation("根据id查询章节")
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",chapter);
    }

    @ApiOperation("修改章节")
    @PutMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapterForm chapterForm){
        EduChapter chapter = new EduChapter();
        BeanUtils.copyProperties(chapterForm,chapter);
        boolean updateResult = chapterService.updateById(chapter);
        if (!updateResult){
            throw GuliException.from(EduResultCode.UPDATE_ERROR);
        }
        return R.ok();

    }

    @ApiOperation("根据id删除章节")
    @DeleteMapping("/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        chapterService.deleteChapter(chapterId);
        return R.ok();
    }

}